﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calendar.Domain
{
    public delegate void UpdateRecordHandler(string record);
    public class WorkRecordHandler
    {
        public UpdateRecordHandler UpdateRecordEvent = null;
        List<WorkRecord> _workRecordList;
        WorkRecord _currentRecord;
        TimeCalculator _timeCalculator;
        FileIO _file;
        string _name;

        public WorkRecordHandler(string name)
        {
            _workRecordList = new List<WorkRecord>();
            _timeCalculator = new TimeCalculator();
            _file = new FileIO();
            _name = name;
        }

        public void CreateRecord(int timeSlot, int breakTime)
        {
            _currentRecord = new WorkRecord();
            _currentRecord.Name = _name;
            _currentRecord.TimeSlot = timeSlot;
            _currentRecord.BreakTime = breakTime;
        }

        public void AddRecord()
        {
            _currentRecord.EndTime = DateTime.Now;
            _currentRecord.TotalTime = _timeCalculator.GetTotalTime(_currentRecord.StartTime, _currentRecord.EndTime);
            _workRecordList.Add(_currentRecord);
            SaveRecord();
            UpdateRecord();
        }

        public void DeleteRecord(int index)
        {
            _workRecordList.RemoveAt(index);
            SaveRecord();
        }

        public void UpdateRecord()
        {
            string startTime = _currentRecord.StartTime.ToShortDateString() + " " + _currentRecord.StartTime.ToString("HH:mm");
            string endTime = _currentRecord.EndTime.ToShortDateString() + " " + _currentRecord.EndTime.ToString("HH:mm");
            string totalTime = _currentRecord.TotalTime.ToString("0.0");
            string record = String.Format("{0}~{1} 共{2}小時 ({3}:{4})", startTime, endTime, totalTime, _currentRecord.TimeSlot, _currentRecord.BreakTime);
            UpdateRecordEvent.Invoke(record);
        }

        public void LoadRecord()
        {
            foreach(WorkRecord record in _file.LoadRecord(_name))
            {
                _currentRecord = record;
                _workRecordList.Add(_currentRecord);
                UpdateRecord();
            }  
        }

        public void SaveRecord()
        {
            _file.SaveRecord(_workRecordList);
        }

        public int GetTimeSlot(int index)
        {
            if (index >= 0)
            {
                return _workRecordList[index].TimeSlot;
            }
            return 25;
        }

        public int GetBreakTime(int index)
        {
            if (index >= 0)
            {
                return _workRecordList[index].BreakTime;
            }
            return 5;
        }

        public int RecordSelectedIndex
        {
            get
            {
                if (_workRecordList.Count > 0)
                {
                    return _workRecordList.Count - 1;
                }
                return -1;
            }
        }

        public string TotalTimeLabel
        {
            get
            {
                double _sum = 0;
                foreach(WorkRecord record in _workRecordList)
                {
                    _sum += record.TotalTime;
                }
                return "總計: " + _sum.ToString("0.0") + " 小時";
            }

        }
    }
}
